package com.imooc.rabbitmq.returnlistener;

import com.imooc.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ReturnListener;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    private static final String exchangeName = "test_return_exchange";
    private static final String routingKey = "return.save";
    private static final String routingKeyError = "abc.save";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        String msg = "Hello RabbitMQ Return Message";

        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("-------------------Handle return--------------------");
                System.out.println("replayCode:"+replyCode);
                System.out.println("replyText:"+replyText);
                System.out.println("exchange:"+exchange);
                System.out.println("routingKey:"+routingKey);
                System.out.println("properties:"+properties);
                System.out.println("body:"+new String(body));
            }
        });
        //channel.basicPublish(exchangeName,routingKey,true,null,msg.getBytes());
        channel.basicPublish(exchangeName,routingKeyError,true,null,msg.getBytes());
    }
}
